<?php
# @version SVN: $Id: tags.class.inc 570 2012-03-14 20:17:55Z novachok $


require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_tags extends fields_common
{
   public $independed = TRUE;


   public function value($lang = NULL)
   {
   }

   /**
    * Normalize Value
    *
    * @param mixed $data
    * @param string $alias
    * @param array $article
    */
   public function normalizeValue()
   {
   }


   public function saveValue($lang = NULL)
   {
   }


   
   public function getFieldHTML($data)
   {
      $value = $data->{$this->alias}(null);
      $tags  = tags::getAllTags();
      if(!empty($tags))
      {
         $tgs = array();
         foreach($tags AS $tg)
         {
            $tgs[] = addslashes($tg['title']);
         }
         $tags = '"'. join('","', $tgs) .'"';
      } else
      {
         $tags = '';
      }

      $html = '';
      $html .= '<input name="f['. $this->alias .']" id="f_'. $this->alias .'" value="'. (!empty($value) ? join(',', $value) : '') .'" />';
      $html .= '<script type="text/javascript" src="/lib/js/jquery.tagsinput.js"></script>';
      $html .= '<script type="text/javascript">';
      $html .= '$("#f_'. $this->alias .'").tagsInput({
            width: "50%",
            height: "70px",
            defaultText: "'. htmlspecialchars(gettext::translate('meccano', 'addtag')) .'",
            autocomplete_url: "/api.json?method=tags.get",
            autocomplete: { source: ['. $tags .'] }
         });';
      $html .= '</script>';

      return $html;
   }

   public function validateInput($value, $dataObject)
   {
      return tags::setAssignedTags($dataObject->trueID(), $value);
   }
}